package com.example.myapplication.module_notification;

import static android.content.ContentValues.TAG;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.data.Notification;

import java.util.List;

public  class NotificationsAdapter extends BaseAdapter implements AbsListView.OnScrollListener {

    private List<Notification> data;
    private LayoutInflater layoutInflater;
    private Context context;


    public NotificationsAdapter(Context context, List<Notification> data) {
        this.context = context;
        this.data = data;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {

    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        Log.d(TAG, "onScroll: "+i+"  "+"  "+i1+"  "+i2);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    /**
     * 获得某一位置的数据
     */
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    /**
     * 获得唯一标识
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            //获得组件，实例化组件
            convertView = layoutInflater.inflate(R.layout.layout_for_index_display, null);
            viewHolder.cover_img = convertView.findViewById(R.id.cover_of_item);
            viewHolder.title = convertView.findViewById(R.id.tv_title);
            viewHolder.who_pub = convertView.findViewById(R.id.tv_who);
            viewHolder.pub_date = convertView.findViewById(R.id.tv_date);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //绑定数据
        Glide.with(context).load(this.data.get(position).getCoverUrl()).into(viewHolder.cover_img);
        viewHolder.title.setText(this.data.get(position).getTitle());
        viewHolder.who_pub.setText(this.data.get(position).getWhoPub().getName());
        String pubdate = this.data.get(position).getPubTime();
        pubdate = pubdate.substring(0,pubdate.lastIndexOf('T'));
        viewHolder.pub_date.setText(pubdate);

        convertView.setOnClickListener(view -> {
            Intent i = new Intent(context, DetailOfNotificationActivity.class);
            i.putExtra("noti",this.data.get(position));
            context.startActivity(i);
        });
        return convertView;
    }

    /**
     * 组件集合，对应list.xml中的控件
     *
     * @author Administrator
     */
    public final class ViewHolder {
        public ImageView cover_img;
        public TextView title;
        public TextView who_pub;
        public TextView pub_date;
    }

}